package com.pucheng.bracelet.ui.adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.provider.MediaStore
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.pucheng.bracelet.databinding.ItemGridMenuBinding
import com.pucheng.bracelet.ui.activity.SettingActivity
import com.pucheng.bracelet.ui.activity.clock.ClockActivity
import com.pucheng.bracelet.ui.activity.message.MsgPushActivity
import com.pucheng.bracelet.ui.activity.upgrade.UpgradeActivity
import com.pucheng.bracelet.ui.activity.watch.WatchActivity


class BraceletMenuAdapter :
    ListAdapter<MenuData, MenuViewHolder>(object : DiffUtil.ItemCallback<MenuData>() {
        override fun areItemsTheSame(oldItem: MenuData, newItem: MenuData): Boolean =
            oldItem.name == newItem.name

        override fun areContentsTheSame(oldItem: MenuData, newItem: MenuData): Boolean =
            oldItem == newItem

    }) {

    var onItemClickListener:OnItemClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MenuViewHolder {
        return MenuViewHolder(
            ItemGridMenuBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            ),
            parent.context,onItemClickListener!!
        )
    }

    override fun onBindViewHolder(holder: MenuViewHolder, position: Int) {
        val item = getItem(position)
        item?.apply {
            holder.bind(this)
        }
    }

    override fun submitList(list: MutableList<MenuData>?) {
        super.submitList(if (list != null) ArrayList(list) else null)
    }

    interface OnItemClickListener{
        fun onItemClick(item:Any)
    }

}

class MenuViewHolder(private val binding: ItemGridMenuBinding, private val context: Context,private val onItemClickListener: BraceletMenuAdapter.OnItemClickListener) :
    RecyclerView.ViewHolder(binding.root) {
    private var menuData: MenuData? = null

    init {
        binding.setOnClickListener {
            if(onItemClickListener != null){
                menuData?.name?.let { it1 -> onItemClickListener.onItemClick(it1) }
            }
        }
    }

    fun bind(menuData: MenuData) {
        this.menuData = menuData
        binding.apply {
            menu = menuData
            executePendingBindings()
        }
    }

}

data class MenuData(
    val iconId: Int,
    val name: String
)
